<?php
// $Id: admin/index.php,v 1.22 2005/02/06 C. Felix AKA the Cat
//  ------------------------------------------------------------------------ //
//             XF Guestbook                                                  //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //

include_once '../../../include/cp_header.php';
include_once '../include/cp_functions.php';
include_once("../class/msg.php");

if (isset( $_POST))
    foreach ( $_POST as $k => $v ) { ${$k} = $v;} 
	
if (isset( $_GET))
    foreach ( $_GET as $k => $v ) { ${$k} = $v;} 

if (isset($_GET['op']))  $op = $_GET['op'];
	
if (isset($_POST['op'])) $op = $_POST['op'];

$start = ( isset($_GET['start'])) ? intval($_GET['start']) : 0;
	
if (!isset($op)) $op = 'show';
if (!isset($action)) $action = 'all';

$msg_handler =& xoops_getmodulehandler('msg');

function delete($msg_id) {
	global $msg_handler;
   	$idmsg =  isset($_POST['idmsg']) ? intval($_POST['idmsg']) : intval($_GET['idmsg']);
	$ok =  isset($_POST['ok']) ? intval($_POST['ok']) : 0;
    if ( $ok == 1 ) {
		$msg = & $msg_handler->get($msg_id);
		$del_msg_ok = $msg_handler->delete($msg);
		$filename = $msg->getVar('photo');
		if($filename != '') {
			$filename = XOOPS_ROOT_PATH.'/modules/xfguestbook/images/msg/'.$filename;
			unlink($filename);
		}
		if ($del_msg_ok){
			$messagesent = _AM_XFGB_MSGDELETED;
		} else {
			$messagesent = _AM_XFGB_ERRORDEL;
		}
        redirect_header("index.php",2,$messagesent);
	} else {
		xoops_cp_header();
		xoops_confirm(array('op' => 'delete', 'idmsg' => $msg_id, 'ok' => 1), 'index.php', _DELETE);
		xoops_cp_footer();
	}
}

function approve($msg_id) {
	global $msg_handler;
	$msg = & $msg_handler->get($msg_id);
	$msg->setVar('moderate', 0);
	if ($msg_handler->insert($msg)) {
			$messagesent = _AM_XFGB_VALIDATE;
	} else {
		$messagesent = _AM_XFGB_ERRORVALID;
	}
	
	redirect_header("index.php?action=waiting",2,$messagesent);
	exit();
}

function save($idmsg, $user_id,$uname,$email,$url,$title,$message,$note,$gender,$country,$moderate, $del_img) {
	global $msg_handler;
		
	$msg = $msg_handler->get($idmsg);
	$msg->setVar('msg_id', $idmsg);
	$msg->setVar('user_id', $user_id);
	$msg->setVar('uname', $uname);
	$msg->setVar('email', $email);
	$msg->setVar('url', $url);
	$msg->setVar('title', $title);
	$msg->setVar('message', $message);
	$msg->setVar('note', $note);
	$msg->setVar('gender', $gender);
	$msg->setVar('country', $country);
	$msg->setVar('moderate', $moderate);
	if($del_img) {
		$filename = XOOPS_ROOT_PATH.'/modules/xfguestbook/images/msg/'.$msg->getVar('photo');
		unlink($filename);
		$msg->setVar('photo', '');
	}
	if ($msg_handler->insert($msg)) 
		redirect_header("index.php?op=show",1,_AM_XFGB_MSGMOD);
	else
		redirect_header("index.php?op=show",1,_AM_XFGB_MSGERROR);
	exit();
}

function edit($idmsg) {
	global $msg_handler;
	global $xoopsModule;
	$msg = & $msg_handler->get($idmsg);
   
	include "../include/form_edit.inc.php";
	$msg_form->display();
}

function show() {
	global $action, $start, $msg_handler, $xoopsModule;
	$limit = 10;

		echo "<table width='100%' border='0' cellpadding ='2' cellspacing='1' class='outer'>";
		echo "<tr><td class='even'>";
		if ($action == 'all') {
			msg_menu(0);
			$title = _AM_XFGB_ALLMSG;
			$criteria = new Criteria('msg_id',0,'>');
			$criteria->setSort('post_time');
		}
		if ($action == 'published') {
			msg_menu(1);
			$title = _AM_XFGB_PUBMSG;
			$criteria = new Criteria('moderate', '0');
			$criteria->setSort('post_time');
		}
		if ($action == 'waiting') {
			msg_menu(2);
			$title = _AM_XFGB_WAITMSG;
			$criteria = new Criteria('moderate', '1');
			$criteria->setSort('post_time');
		}
			
    $totalcount = $msg_handler->getCount($criteria);
	$criteria->setOrder('DESC');
	$criteria->setLimit($limit);
	$criteria->setStart($start);
	$msg =& $msg_handler->getObjects($criteria);
			
		echo '<b>'._AM_XFGB_NBMSG.$totalcount."</b></td></tr></tr></td></table>";

		echo "<table border='1' width='100%' cellpadding ='2' cellspacing='1'>";
    	echo "<tr class='bg3'>";
		echo "<td align='center'></td>";
		echo "<td align='center'><b>"._AM_XFGB_NAME."</td>";
		echo "<td align='center'><b>"._AM_XFGB_TITLE."</td>";
		echo "<td align='center'><b>"._AM_XFGB_MESSAGE."</td>";
		echo "<td align='center'><b>"._AM_XFGB_DATE."</td>";
		echo "<td align='center'><b>"._AM_XFGB_ACTION."</td></b>";
		echo "</tr>";

		if ($totalcount == '0') echo "<tr ><td align='center' colspan ='10' class = 'head'><b>"._AM_XFGB_NOMSG."</b></td></tr>";

		foreach( $msg as $onemsg ){			
			$all_msg = array();
			$all_msg['post_time'] = formatTimestamp($onemsg->getVar('post_time'));
        	$all_msg['msg_id'] = $onemsg->getVar('msg_id');
            $all_msg['user'] = ($onemsg->getVar('user_id') > 0) ? xoops_getLinkedUnameFromId($onemsg->getVar('user_id')) : $onemsg->getVar('uname');
            $all_msg['action'] = "<a href='index.php?op=edit&amp;idmsg=".$onemsg->getVar('msg_id')."'>"._EDIT."</a><br>";
            $all_msg['action'] .= "<a href='index.php?op=delete&amp;idmsg=".$onemsg->getVar('msg_id')."'>"._DELETE."</a>";
			$img_status = "<img src='".XOOPS_URL."/modules/".$xoopsModule->dirname()."/images/";
			if ($onemsg->getVar('moderate')){
            	$all_msg['action'] .= "<br><a href='index.php?op=approve&amp;idmsg=".$onemsg->getVar('msg_id')."'>"._AM_XFGB_PUB."</a>";
				$img_status .= "ic15_question.gif'>";
			} else {
				$img_status .= "ic15_ok.gif'>";
			}
        	$all_msg['title'] = "<a href='../index.php?op=show_one&id=".$onemsg->getVar('msg_id')."'>".$onemsg->getVar('title')."</a>";
 			$all_msg['message'] = $onemsg->getVar('message');
			
		if ($onemsg->getVar('photo'))
			$all_msg['message'] ="<img src=\"".XOOPS_URL."/modules/".$xoopsModule->dirname()."/images/msg/".$onemsg->getVar('photo')."\" align = \"left\" hspace =\"10\">".$onemsg->getVar('message');
		else
 			$all_msg['message'] = $onemsg->getVar('message');
			
			
      		echo "<tr><td align='center' class = 'head'><b>".$img_status."</b>";
        	echo "</td><td align='center' class = 'even'>".$all_msg['user']."";
        	echo "</td><td align='left' class = 'odd'>".$all_msg['title']."";
        	echo "</td><td align='left' class = 'even'>".$all_msg['message']."";
			echo "</td><td align='center' class='odd'>".$all_msg['post_time']."";
			echo "</td><td align='center' class='even'>".$all_msg['action']."";

       		echo "</td></tr>";
        	unset($all_msg);
    	}

    	echo "</table><br />";

		if ( $totalcount > $limit ) {
			include_once XOOPS_ROOT_PATH.'/class/pagenav.php';
			$pagenav = new XoopsPageNav($totalcount, $limit, $start, 'start', 'action='.$action);
			echo "<div style='text-align: center;' class = 'head'>".$pagenav->renderNav()."</div><br />";
     	} else {
			echo '';
		}
    	echo"<br />";
}

switch ($op) {
	case "save":
		save($idmsg, $user_id,$uname,$email,$url,$title,$message,$note,$gender,$country,$moderate,$del_img);
		break;
	case "edit":
		xoops_cp_header();
		admin_menu();
		edit($idmsg);
		xoops_cp_footer();
		break;
	case "approve":
		approve($idmsg);
		break;
	case "delete":
		delete($idmsg);
		break;
	case "show":
	default:
		xoops_cp_header();
		admin_menu(0);
		show();
		xoops_cp_footer();
	break;
}
?>
